package UILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModelLayer.*;


/**
 * This is the text ui to the student enrollment system
 * 
 * @author (Gunhild Andersen) 
 * @version 2013.10.02 KBHA changes of language, functionality
 * 
 */
public class StudentUI
{
    // instance variables
    private StudentCtr studentCtr;

    /**
     * constructor
     */
    public StudentUI()
    {
        // initialization
        studentCtr = new StudentCtr();
    }

    public void start()
    {
       
        studentMenu();
    }
    private void studentMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeStudentMenu(); 
          if(choise == 1)
          {   //create  studerende
              createStudent();
              
          }else{
              if(choise == 2)
              { findStudent();
               }
              else{
                  if(choise == 3)
                  {
                      System.out.println("the system will stop");
                      exit = true;
                    }
                    else{
                        exit = true;            
              
                    }//end else
                }//end else
          }//end else
            
       }//end while
       
    }
    private int writeStudentMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f*** Student Menu ***");
            System.out.println(" (1) Create student");
            System.out.println(" (2) Find student");
            System.out.println(" (3) End the system");
            System.out.print("\n Type your choice: ");
            
            int choise = keyboard.nextInt();
         
            return choise;
        }
        //the method reads id for a person and return
    // 
    private int inputStudentID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Type the student id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You have to type a number, please try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    private String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Type the students name: ");
         String name = keyboard.nextLine();
         return name;
    }
   
         
    private String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
     private String  inputMail()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type the e-mail: ");
        String mail = keyboard.nextLine();
        return mail;
    }
    private void showId(int id)
    {
        System.out.println("ID: " + id);
    }
    private void showName(String name)
    { 
        System.out.println("Name: " + name);
    }
    private void showAddress(String address)
    { 
        System.out.println("Address: " + address);
    }

    private void showMail(String mail)
    { 
        System.out.println("E-mail: " + mail);
    }
    
    private void findStudent()
    {//is to be changed when the address book controller is completed
        int id = inputStudentID();
        Student s = (studentCtr.findStudent(id));
        if(s != null)
        {
          showName(s.getName());
          showAddress(s.getAddress());
          showMail(s.getEmail());
        }
        else{
            System.out.println("No student with that number");
        }
        pause();
       
    } 
    private void pause()
    {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Enter to continue");
         String address = keyboard.nextLine();
    }
    
    private void createStudent()
    
    {
        int id = inputStudentID();
        String name = inputName();
        String address = inputAddress();
        String mail = inputMail();
        
        
        studentCtr.createStudent(id, name, address, mail);
    }   
}
